package com.supreme.orderserver.handler;

import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.utils.AjaxResp;
import feign.FeignException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BizException.class)
    public AjaxResp exceptionHandler(HttpServletRequest request, HttpServletResponse response, BizException exception) {
        int code = exception.getCode();
        String message = exception.getMessage();
        response.setStatus(code);
        return new AjaxResp(code, message);
    }

    @ExceptionHandler(value = FeignException.class)
    public AjaxResp exceptionHandler(HttpServletRequest request, HttpServletResponse response, FeignException exception) {
        response.setStatus(exception.status());
        String message = exception.getMessage();
        return new AjaxResp(message);
    }

    @ExceptionHandler(value = NullPointerException.class)
    public AjaxResp exceptionHandler(HttpServletRequest request, HttpServletResponse response, NullPointerException exception) {
        return new AjaxResp(exception.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public AjaxResp exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
        return new AjaxResp(exception.getMessage());
    }
}
